#!/usr/bin/env python
# _*_coding:utf-8_*_

"""
@Time :    2019/10/5 7:11
@Author:  ThinHeader
@File: bakend.py
@Software: PyCharm
"""
from account.models import User
from common.views import get_obj


class CustomBackend(object):
    def authenticate(self, username=None, openid=None, password=None, **kwargs):
        if username:
            try:
                user = User.objects.get(username=username, status=User.STATUS_VALID)

                if user.check_password(password):
                    return user
            except User.DoesNotExist:
                return None
        elif openid:
            return get_obj(User, gh_openid=openid, status=User.STATUS_VALID)
        elif 'unionid' in kwargs:
            return get_obj(User, unionid=kwargs['unionid'], status=User.STATUS_VALID)
        else:
            return None
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(id=user_id, status=User.STATUS_VALID)
        except User.DoesNotExist:
            return None